{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# step 1. get file's name and labels list \n",
    "# step 2. (optional) shuffle and set epochs\n",
    "# step 3. put file list into queues\n",
    "# step 4. files pre operations (like images crop)\n",
    "# step 5. set batchs (threads)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# import tools\n",
    "%matplotlib inline\n",
    "import tensorflow as tf\n",
    "import os\n",
    "import glob\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# return nohiden files dir\n",
    "def listdir_nohidden(path):\n",
    "    return glob.glob(os.path.join(path, '*'))\n",
    "# read filesname and labels list\n",
    "def read_list_from_disk():\n",
    "    filenameslist = []\n",
    "    filelabelslist = []\n",
    "    # 5 styles\n",
    "    styles = ['篆书','隶书','楷书','行书','草书']\n",
    "    for style_item in styles:\n",
    "        dir_name = './images/'+style_item\n",
    "        for files in os.listdir(dir_name):\n",
    "            # file name \n",
    "            filenameslist.append( dir_name+'/'+files )\n",
    "            # file label\n",
    "            filelabelslist.append(styles.index(style_item))\n",
    "    return filenameslist,filelabelslist\n",
    "\n",
    "# image widths and heights\n",
    "IMG_WIDTHS = 100\n",
    "IMG_HEIGHTS = 100\n",
    "\n",
    "# image_operation\n",
    "def image_operate(input_queue):\n",
    "    \n",
    "    label = input_queue[1]\n",
    "    # get contents from file\n",
    "    contents = tf.read_file(input_queue[0])\n",
    "    # decode image by its format\n",
    "    image = tf.image.decode_gif(contents)\n",
    "    # resize \n",
    "    image = tf.image.resize_images(images=image,\n",
    "                                   size=[IMG_WIDTHS,IMG_HEIGHTS])\n",
    "    # rgb to grayscale\n",
    "    image = tf.image.rgb_to_grayscale(images=image)\n",
    "    \n",
    "    image = tf.reshape(image,tf.stack([IMG_WIDTHS,IMG_HEIGHTS,1]))\n",
    "    \n",
    "    return image,label\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get list of filename and labels\n",
    "filenameslist,filelabelslist = read_list_from_disk()\n",
    "# convert python list to tensor list\n",
    "filenameslist_tensor = tf.convert_to_tensor(filenameslist,\n",
    "                                            dtype=tf.string)\n",
    "filelabelslist_tensor = tf.convert_to_tensor(filelabelslist,\n",
    "                                             dtype=tf.int16)\n",
    "\n",
    "# partition list into train and test parts\n",
    "partitions = [0]*len(filelabelslist)\n",
    "testfilesize = len(filelabelslist)/3\n",
    "partitions[:testfilesize] = [1]*testfilesize\n",
    "random.shuffle(partitions)\n",
    "\n",
    "# dynamic partition\n",
    "trainfilelist,testfilelist = tf.dynamic_partition(data=filenameslist_tensor,\n",
    "                                                  partitions=partitions,\n",
    "                                                  num_partitions=2)\n",
    "trainlabellist,testlabellist = tf.dynamic_partition(data=filelabelslist_tensor,\n",
    "                                                    partitions=partitions,\n",
    "                                                    num_partitions=2)\n",
    "\n",
    "# train and test queue\n",
    "train_input_queue = tf.train.slice_input_producer(tensor_list=[trainfilelist,trainlabellist],\n",
    "                                                  shuffle=False,num_epochs=2)\n",
    "test_input_queue = tf.train.slice_input_producer(tensor_list=[testfilelist,testlabellist],\n",
    "                                                 shuffle=False,num_epochs=2)\n",
    "\n",
    "# images operations\n",
    "trainfile , trainlabel = image_operate(train_input_queue)\n",
    "testfile, testlabel = image_operate(test_input_queue)\n",
    "\n",
    "\n",
    "# batch size \n",
    "BATCH_SIZE = 2\n",
    "\n",
    "# set batch\n",
    "train_batch = tf.train.batch(tensors=[trainfile , trainlabel],\n",
    "                             batch_size=BATCH_SIZE,\n",
    "                             num_threads=2)\n",
    "test_batch = tf.train.batch(tensors=[testfile, testlabel],\n",
    "                            batch_size=BATCH_SIZE,\n",
    "                            num_threads=2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3]\n",
      "[4 2]\n",
      "[3 4]\n",
      "OutOfRangeError\n",
      "OutOfRangeError\n",
      "OutOfRangeError\n",
      "OutOfRangeError\n",
      "OutOfRangeError\n",
      "OutOfRangeError\n",
      "OutOfRangeError\n"
     ]
    }
   ],
   "source": [
    "# session\n",
    "with tf.Session() as session:\n",
    "    # initial all variables\n",
    "    ini_op = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())\n",
    "    session.run(ini_op)\n",
    "    # start queues or process will be jammed\n",
    "    # set threads coordinator\n",
    "    coordinator = tf.train.Coordinator()\n",
    "    threads = tf.train.start_queue_runners(sess=session,\n",
    "                                           coord=coordinator)\n",
    "    for i in range(10):\n",
    "        try:\n",
    "            files,label = session.run(test_batch)\n",
    "            print label\n",
    "        except Exception as ex:\n",
    "            print type(ex).__name__ \n",
    "            \n",
    "    coordinator.request_stop()\n",
    "    coordinator.join(threads)\n",
    "    session.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
